<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class RankCache extends Base
{
    protected $user_collect_rank = 'COLLECT:GIFT'; //收
    protected $user_send_rank = 'SEND:GIFT';//送
    protected $room_rank = 'ROOM:RANK';

    protected $table = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function incRank($user_id, $to_user_ids = [], $room_id = 0, $score)
    {
        $user_collect_rank = $this->user_collect_rank;
        $user_send_rank = $this->user_send_rank;
        $room_rank = $this->room_rank;
        Redis::zincrby($user_send_rank, $score, $user_id);
        if ($to_user_ids) {
            foreach ($to_user_ids as $v) {
                Redis::zincrby($user_collect_rank, $score, $v);
            }
        }
        if ($room_id != 0) {
            Redis::zincrby($room_rank, $score, $room_id);
        }
        return true;
    }


    public function roomRank($room_id)
    {
        return Redis::zscore($this->room_rank, $room_id);
    }

    //用户收
    public function collectRank($user_id)
    {
        return Redis::zscore($this->user_collect_rank, $user_id);
    }

    //用户送
    public function sendRank($user_id)
    {
        return Redis::zscore($this->user_send_rank, $user_id);
    }

    //用户总榜
    public function allSendRank()
    {
        return Redis::zrevrange($this->user_send_rank, 0, 4);
    }

    //用户收总榜
    public function allCollectRank()
    {
        return Redis::zrevrange($this->user_collect_rank, 0, 4);
    }

    //房间收总榜
    public function allRoomRank()
    {
        return Redis::zrevrange($this->room_rank, 0, 2);
    }


    public function createNewCacheKey()
    {
        $send_gift_93 = "select from_user_id,sum(gift_num) as sum_score from zb_gift_stream where gift_id = 93 group by from_user_id";

        $send_gift_94 = "select from_user_id,sum(gift_num)  as sum_score from zb_gift_stream where gift_id = 94 group by from_user_id";

        $collect_gift_93 = "select user_id,sum(gift_num)  as sum_score from zb_gift_stream where gift_id = 93 group by user_id";

        $collect_gift_94 = "select user_id,sum(gift_num)  as sum_score from zb_gift_stream where gift_id = 94 group by user_id";

        $room_gift_93 = "select room_id,sum(gift_num)  as sum_score from zb_gift_stream where gift_id = 93 and room_id != 0 group by room_id";

        $room_gift_94 = "select room_id,sum(gift_num)  as sum_score from zb_gift_stream where gift_id = 94 and room_id != 0 group by room_id";

        $send_gift_93_sql = DB::connection('mysql_kkaudio')->select($send_gift_93);
        $send_gift_94_sql = DB::connection('mysql_kkaudio')->select($send_gift_94);
        $collect_gift_93_sql = DB::connection('mysql_kkaudio')->select($collect_gift_93);
        $collect_gift_94_sql = DB::connection('mysql_kkaudio')->select($collect_gift_94);
        $room_gift_93_sql = DB::connection('mysql_kkaudio')->select($room_gift_93);
        $room_gift_94_sql = DB::connection('mysql_kkaudio')->select($room_gift_94);
        //93 -- 1   94 -- 4
        $user_collect_rank = $this->user_collect_rank;
        $user_send_rank = $this->user_send_rank;
        $room_rank = $this->room_rank;

        foreach($send_gift_93_sql as $v1){
            $score = $v1->sum_score *1;
            Redis::zincrby($user_send_rank,$score , $v1->from_user_id);
        }

        foreach($send_gift_94_sql as $v2){
            $score = $v2->sum_score *4;
            Redis::zincrby($user_send_rank,$score , $v2->from_user_id);
        }

        foreach($collect_gift_93_sql as $v3){
            $score = $v3->sum_score *1;
            Redis::zincrby($user_collect_rank,$score , $v3->user_id);
        }

        foreach($collect_gift_94_sql as $v4){
            $score = $v4->sum_score *4;
            Redis::zincrby($user_collect_rank,$score , $v4->user_id);
        }

        foreach($room_gift_93_sql as $v5){
            $score = $v5->sum_score *1;
            Redis::zincrby($room_rank,$score , $v5->room_id);
        }

        foreach($room_gift_94_sql as $v5){
            $score = $v5->sum_score * 4;
            Redis::zincrby($room_rank,$score , $v5->room_id);
        }
        $res['send_user'] = Redis::zrevrange($user_send_rank, 0, 4);
        $res['collect_user'] = Redis::zrevrange($user_collect_rank, 0, 4);
        $res['room'] = Redis::zrevrange($room_rank, 0, 2);
        dd($res);
    }

}